Ground state simulation of the quantum Ising model.

# pylint: disable=invalid-name

import numpy as np
import numpy.linalg as nla

import qtealeaves as qtl
from qtealeaves.models import get_quantum_ising_1d


def main(tn_type=5, statics_method=2, input_folder=None, output_folder=None):
    """
    Main method for the ground state simulation of 1d quantum
    Ising model.

    **Arguments**

    tn_type : int, optional
        Choose 5 for python-TTN, 6 for python-MPS.
        Default to 5.

    statics_method : integer, optional
        Method to run ground state search for this/all iteration.
        0 : default (2)
        1 : sweep
        2 : sweep with space expansion (can still be reduced to sweep
            during the simulation based on a energy condition)

    input_folder : str | None, optional
        Input folder. Default to None.

    output_folder : str | None, optional
        Output folder. Default to None.
    """
    if input_folder is None:
        input_folder = lambda params: "QI1d/input_L%d" % (params["L"])
    if output_folder is None:
        output_folder = lambda params: "QI1d/output_L%d" % (params["L"])

    model, my_ops = get_quantum_ising_1d()

    my_conv = qtl.convergence_parameters.TNConvergenceParameters(
        max_iter=7, max_bond_dimension=20, statics_method=statics_method
    )

    my_obs = qtl.observables.TNObservables()
    my_obs += qtl.observables.TNObsLocal("<sz>", "sz")
    my_obs += qtl.observables.TNObsLocal("<sx>", "sx")

    simulation = qtl.QuantumGreenTeaSimulation(
        model,
        my_ops,
        my_conv,
        my_obs,
        tn_type=tn_type,
        folder_name_input=input_folder,
        folder_name_output=output_folder,
        has_log_file=False,
        store_checkpoints=False,
    )

    params = []

    params.append({"L": 8, "J": 1.0, "g": 0.5})

    simulation.run(params, delete_existing_folder=True)

    for elem in params:
        # Without symmetries, we find the global sector
        energy_0 = nla.eigh(model.build_ham(my_ops, elem))[0][0]

        results = simulation.get_static_obs(elem)
        print("TN energies E0", results["energy"])
        print("ED energies E0", energy_0)

        # Run one assert that we are converged sufficiently
        assert np.abs(energy_0 - results["energy"]) < 1e-4

    print(
        f"\nExample `{__file__}` ran successfully; "
        + "ground state energy at least correct up to 1e-4."
    )

    return


if __name__ == "__main__":
    main()

Gallery generated by Sphinx-Gallery